package com.ys.controller.web.hr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.entity.sys.TsUserChange;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.dto.NameValueSelectDto;
import com.bo.enume.sys.StatusEnum;
import com.bo.vo.W;
import com.ys.config.MenuId;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.service.sys.CommonApiService;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/hr/thrOperateCompanyInfo")
public class WebThrOperateCompanyInfoController {
	
	@Autowired
	private CommonApiService<ThrOperateCompanyApply> commonService;
	
	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		try {
			Map<String,Object> entity = new ThrOperateCompanyApply()
					.addLeftJoin("TS_UserChange B on B.Id = A.OperatorChangeId")
					.addField("B.UserId operatorUserId")//运营人工号
					.addField("B.UserName operatorUserName")//运营人
					.addField("B.RoleName roleName")//运营人岗位
					.addField("B.DeptName deptName")//运营人部门

					.addLeftJoin("TS_OperateOrg O on O.Id = A.OperateOrgId")
					.addLeftJoin("THR_OperateCompanyApply PO ON O.HeadVgsbm = PO.Vgsbm and 1=1")
					.addField("O.Name orgName")//机构名称
					.addField("PO.Vgsbm AS orgbm")//上级公司编码
					.addField("PO.Vgsjc AS orgJc")//上级公司简称
					.setId(id)
					.queryFirstMap();

			Map<String, Object> extraParam = new HashMap<String, Object>();
			//关联数据
			if(entity.get("operatorUserId")!=null)extraParam.put("operatorUserId", entity.get("operatorUserId"));
			if(entity.get("operatorUserName")!=null)extraParam.put("operatorUserName", entity.get("operatorUserName"));
			if(entity.get("roleName")!=null)extraParam.put("roleName", entity.get("roleName"));
			if(entity.get("deptName")!=null)extraParam.put("deptName", entity.get("deptName"));
			if(entity.get("orgName")!=null)extraParam.put("orgName", entity.get("orgName"));
			if(entity.get("orgbm")!=null)extraParam.put("orgbm", entity.get("orgbm"));
			if(entity.get("orgJc")!=null)extraParam.put("orgJc", entity.get("orgJc"));

			return commonService.toEdit(model, id, new ThrOperateCompanyApply(), MenuId.THR_OperateCompanyApply, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 修改运营人
	 */
	@RequiresPermissions("thrOperateCompanyInfo:editOperator")
	@GetMapping("/toEditOperator")
	public String toEditOperator(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		
		ThrOperateCompanyApply entity = new ThrOperateCompanyApply().setId(id).queryFirst();
		TsUserChange tsUserChange = new TsUserChange().setId(entity.getOperatorChangeId());
		String cUserOld = entity.getCuser();
		String cUserNameOld = tsUserChange.getUserName();
		ToEditOperatorDto dto = new ToEditOperatorDto(id, cUserOld, cUserNameOld);
		model.addAttribute("dto", dto);
		
		return "hr/thrOperateCompanyInfo/toEditOperator";
	}
	@Data
	@AllArgsConstructor
	private static class ToEditOperatorDto{
		private Long id; // ID
		private String cUserOld; // 旧运营人工号
		private String cUserNameOld; // 旧运营人姓名
	}
	
	/*
	 * 修改运营状态
	 */
	@RequiresPermissions("thrOperateCompanyInfo:editStatus")
	@GetMapping("/toEditStatus")
	public String toEditStatus(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		
		ThrOperateCompanyApply entity = new ThrOperateCompanyApply().setId(id).queryFirst();
		String vGsBm = entity.getVgsbm();
		String vGsJc = entity.getVgsjc();
		
		List<NameValueSelectDto> statusArr = new ArrayList<NameValueSelectDto>();
		for(StatusEnum e : StatusEnum.values()) {
			String name = e.name;
			String value = e.value.toString();
			boolean isSelected = entity.getStatus() == e.value;
			statusArr.add(new NameValueSelectDto(name, value, isSelected));
		}
		ToEditStatusDto dto = new ToEditStatusDto(id, vGsBm, vGsJc, statusArr);
		model.addAttribute("dto", dto);
		
		return "hr/thrOperateCompanyInfo/toEditStatus";
	}
	@Data
	@AllArgsConstructor
	private static class ToEditStatusDto{
		private Long id; // ID
		private String vGsBm; // 
		private String vGsJc; // 
		private List<NameValueSelectDto> statusArr;
	}
	
	/**
	 * 批量修改用户所属机构
	 */
	@GetMapping("/toBatchChangeOrg")
	public String toBatchChangeOrg(Model model,
			String ids){

		List<ThrOperateCompanyApply> listTsUserInfo = new ThrOperateCompanyApply().where("A.Id in (" + ids + ")").queryList();
		List<ToBatchChangeOrgDto.RowDto> rows = new ArrayList<ToBatchChangeOrgDto.RowDto>();
		for(ThrOperateCompanyApply entity : listTsUserInfo) {
			Long id = entity.getId();
			String vgsbm = entity.getVgsbm();
			String vgsjc = entity.getVgsjc();
			rows.add(new ToBatchChangeOrgDto.RowDto(id, vgsbm, vgsjc));
		}

		ToBatchChangeOrgDto dto = new ToBatchChangeOrgDto(rows);

		
		model.addAttribute("dto", dto);
		return "hr/thrOperateCompanyInfo/toBatchChangeOrg";
	}
	@Data
	@AllArgsConstructor
	private static class ToBatchChangeOrgDto{
		private List<RowDto> rows;
		@Data
		@AllArgsConstructor
		private static class RowDto{
			private Long id; // 
			private String vgsbm;
			private String vgsjc; // 名称
		}
	}
}
